<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="司机" prop="personName">
        <el-input
          v-model="queryParams.personName"
          placeholder="请输入人员名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车牌号" prop="carNum">
        <el-input
          v-model="queryParams.carNum"
          placeholder="请输入车牌号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工作区域" prop="fenceName">
        <el-input
          v-model="queryParams.fenceName"
          placeholder="请输入工作区域名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="时间范围">
        <el-date-picker
          v-model="daterangeDataTime"
          :picker-options="pickerOptions"
          size="small"
          style="width: 340px"
          value-format="yyyy-MM-dd HH:MM:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['report:carStop:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="carStopList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="left" />
      <el-table-column type="index" label="序号" width="60" align="left" />
      <el-table-column
        label="所属部门单位"
        align="left"
        prop="deptName"
        minWidth="120px"
      />
      <el-table-column
        label="司机"
        align="left"
        prop="personName"
        minWidth="100px"
      />
      <el-table-column
        label="车牌号"
        align="left"
        prop="carNum"
        minWidth="100px"
      />
      <el-table-column
        label="工作区域名称"
        align="left"
        prop="fenceName"
        minWidth="150px"
        show-overflow-tooltip
      />
      <el-table-column
        label="当班状态"
        align="left"
        prop="fenceName"
        minWidth="100px"
      >
        <template slot-scope="scope">
          <el-tag :type="scope.row.fenceName ? 'success' : 'error'">{{
            scope.row.fenceName ? "当班" : "未当班"
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="数据日期"
        align="left"
        prop="dataTime"
        width="120"
      />
      <el-table-column
        label="停留时段"
        align="left"
        prop="workTime"
        width="150"
      />
      <!-- <el-table-column
        label="开始时间"
        align="left"
        prop="startTime"
        width="180"
      />

      <el-table-column
        label="结束时间"
        align="left"
        prop="endTime"
        width="180"
      /> -->
      <el-table-column
        label="怠速时长"
        align="left"
        prop="stopTimeStr"
        width="120"
      />
      <el-table-column label="经度" align="left" prop="lng" width="120" />
      <el-table-column label="纬度" align="left" prop="lat" width="120" />
      <el-table-column
        label="位置"
        align="left"
        prop="address"
        minWidth="180"
        show-overflow-tooltip
      />

      <!-- <el-table-column label="起始油量" align="left" prop="startOil" />
      <el-table-column label="结束油量" align="left" prop="endOil" />
      <el-table-column label="油量消耗" align="left" prop="oilCost" /> -->
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  listCarStop,
  getCarStop,
  delCarStop,
  addCarStop,
  updateCarStop,
} from "@/api/report/carStop";

export default {
  name: "CarStop",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 怠速时长以及油耗表格数据
      carStopList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 油量消耗时间范围
      daterangeDataTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        personName: undefined,
        carNum: undefined,
        fenceName: undefined,
        dataTime: undefined,
        startTime: undefined,
        endTime: undefined,
        stopTime: undefined,
        startOil: undefined,
        endOil: undefined,
        oilCost: undefined,
      },
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now();
        },
        // shortcuts: [{
        //   text: '今天',
        //   onClick(picker) {
        //     picker.$emit('pick', new Date());
        //   }
        // }, {
        //   text: '昨天',
        //   onClick(picker) {
        //     const date = new Date();
        //     date.setTime(date.getTime() - 3600 * 1000 * 24);
        //     picker.$emit('pick', date);
        //   }
        // }, {
        //   text: '一周前',
        //   onClick(picker) {
        //     const date = new Date();
        //     date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
        //     picker.$emit('pick', date);
        //   }
        // }]
      },
    };
  },
  computed: {
    deptId() {
      return this.$store.getters.choosedDeptId;
    },
  },
  watch: {
    deptId: function () {
      this.queryParams.deptId = this.deptId;

      this.getList();
    },
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询怠速时长以及油耗列表 */
    getList() {
      this.loading = true;

      this.queryParams.params = {};
      if (null != this.daterangeDataTime && "" != this.daterangeDataTime) {
        this.queryParams.params["beginDataTime"] = this.daterangeDataTime[0];
        this.queryParams.params["endDataTime"] = this.daterangeDataTime[1];
      }
      listCarStop(this.queryParams).then((response) => {
        this.carStopList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeDataTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.stopId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const stopIds = row.stopId || this.ids;
      this.$modal
        .confirm('是否确认删除"' + (row.orderName || "已勾选") + '"的数据项?')
        .then(() => {
          this.loading = true;
          return delCarStop(stopIds);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "report/carStop/export",
        {
          ...this.queryParams,
        },
        `车辆怠速统计_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
